package Client;

import SQL.Connections;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

//管理员查看车次
public class AdminView extends JFrame{
    private JFrame frame;
    private JButton 修改Button;
    private JButton 返回Button;
    private JPanel j1;
    private JFormattedTextField formattedTextField1;
    private JFormattedTextField formattedTextField2;
    private JFormattedTextField formattedTextField3;
    private JFormattedTextField formattedTextField4;
    private JFormattedTextField formattedTextField5;
    private JFormattedTextField formattedTextField6;
    private JLabel title;
    private JOptionPane jOptionPane;

    public AdminView() {
        返回Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                frame.dispose();
                AdminPage.initialize();
            }
        });
        修改Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String classNo = formattedTextField1.getText();
                String startPoint = formattedTextField3.getText();
                String destination = formattedTextField4.getText();
                String startTime = formattedTextField5.getText();
                classChange(classNo,startPoint,destination,startTime);
                AdminPage.initialize();
                frame.dispose();
            }
        });
    }

    public static void main(String[] args) {


    }
    public static void initialize(String classNo) {
        AdminView adminView = new AdminView();
        adminView.frame = adminView;
        adminView.setContentPane(adminView.j1);
        adminView.setLocationRelativeTo(null);
        adminView.pack();
        adminView.setVisible(true);

        Vector data = new Vector();
        try {
            Connections connections = new Connections();
            Connection con = connections.getConnection();
            Statement stm = con.createStatement();
            String sqlClasses = "select ClassNumber,ClassTrainNumber,ClassDepaturePlace,ClassGoalPlace,ClassDepatureTime from classes where ClassNumber = '"+classNo+"'";
            ResultSet rsClasses = stm.executeQuery(sqlClasses);
            while (rsClasses.next()){
                adminView.formattedTextField1.setText((String) rsClasses.getString("ClassNumber"));
                adminView.formattedTextField2.setText((String) rsClasses.getString("ClassTrainNumber"));
                adminView.formattedTextField3.setText((String) rsClasses.getString("ClassDepaturePlace"));
                adminView.formattedTextField4.setText((String) rsClasses.getString("ClassGoalPlace"));
                adminView.formattedTextField5.setText((String) rsClasses.getString("ClassDepatureTime"));
                adminView.formattedTextField6.setText(String.valueOf(0));
            }
            String sqlTicket = "select * from Ticket  where TClassNumber = '"+classNo+"'";
            ResultSet rsTicket = stm.executeQuery(sqlTicket);
            //rsTicket.last();
            int row = 0;
            while (rsTicket.next()){
                row++;
            }
            adminView.formattedTextField6.setText(String.valueOf(row));
        }catch (Exception e){
            e.printStackTrace();
        }
        adminView.formattedTextField1.setEditable(false);
        adminView.formattedTextField2.setEditable(false);
        adminView.formattedTextField6.setEditable(false);
    }


    private void classChange(String classNo,String startPoint,String destination,String startTime){
        try {

            Connections connections = new Connections();
            Connection con = connections.getConnection();
            Statement stm = con.createStatement();
            String sql = "update Classes set ClassDepaturePlace = '"+startPoint+"' ,ClassGoalPlace = '"+destination+"',ClassDepatureTime = '"+startTime+"' where ClassNumber = '"+classNo+"'";
            int rsClasses = stm.executeUpdate(sql);
            if(rsClasses == 1){
                jOptionPane.showMessageDialog(null, "修改成功", "系统提示", JOptionPane.INFORMATION_MESSAGE);
            }
        }catch (Exception e){
            jOptionPane.showMessageDialog(null, "修改失败", "系统提示", JOptionPane.INFORMATION_MESSAGE);
            System.out.println("数据库访问异常");
            e.printStackTrace();
        }
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        title = new JLabel(new ImageIcon("train.png"));//插入火车图片
    }
}
